<template>
  <div class="xiangqinbox">
    <div class="header">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="$store.state.rout.url.a">{{$store.state.rout.url.name}}</XtxBreadItem>
      </XtxBread>

    </div>
    <div class="main">
      <div class="left">
        <div  class="top" >
         <div    @mouseover="imgsFn" @mousemove="usePreviewImage"  @mouseout="goodsimgs = null" class="imgs" >
           <span class="zhezhao"  v-show="goodsimgs !==null" :style="position" ></span>
            <img ref="targets" :src="imgUrl" alt="" />
         </div>
  <div class="large" v-show="isShow" :style="[{backgroundImage:`url(${imgUrl})`},bgPosition]"></div>
          <ul>
            <li v-for="(item,index) in ress.mainPictures"
            :key="index"
            @mouseover="imgUrl = item">
              <img :src="item" alt="" />
              </li>

          </ul>
        </div>
          <!-- 商品信息 -->
        <div class="buttom">

          <div>
            <p>销量</p>
            <p class="meiyuan">1000</p>
            <a href="">爱上</a>
          </div>
          <span class="xian"></span>

        </div>
      </div>
<div class="reft">
        <!-- 信息 -->
<Xinxi :xinxi="ressss" :resssa="jiages" :resssas="jiagess"/>
<!-- 地区 -->
<diqu />
<!-- 购物车 -->
<gouwuche :gouwuche="gouwuches" @num="jiage" @a="ann" @nums="sumsFn"/>
</div>
    </div>
    <!-- 推荐 -->
    <relevant />
  </div>
</template>

<script>
// import goodsimg from './components/goods-img.vue'
import gouwuche from './components/xiangqin-xinxi.vue'
import Xinxi from './components/goods-xinxi.vue'
import diqu from './components/goods-diqu.vue'
import relevant from './components/goods-relevant.vue'

import { reactive, ref, watch } from 'vue'

import { useMouseInElement } from '@vueuse/core'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { findGoods } from '@/api/category'
const usePreviewImage = () => {
  // 被监听的区域
  const targets = ref(null)
  // 控制遮罩层和预览图的显示和隐藏
  const isShow = ref(false)
  // 遮罩层位置坐标

  const position = reactive({
    left: 0,
    top: 0
  })
  // 右侧预览大图的坐标
  const bgPosition = reactive({
    backgroundPositionX: 0,
    backgroundPositionY: 0
  })
  const { elementX, elementY, isOutside } = useMouseInElement(targets)
  // console.log('我是监听dom', elementX.value, elementY, isOutside)
  // 基于侦听器侦听值的变化
  // console.log(elementX.value, elementY.value, isOutside.value, x.value, useMouseInElement(targets))
  watch([elementX, elementY, isOutside], () => {
    // console.log(elementX.value, elementY.value)
    // 通过标志位控制显示和隐藏
    isShow.value = !isOutside.value
    if (isOutside.value) return
    // X方向坐标范围控制
    if (elementX.value < 50) {
      // 左侧
      position.left = -50
    } else if (elementX.value > 350) {
      // 右侧
      position.left = 250
    } else {
      // 中间
      position.left = elementX.value - 100
    }
    // Y方向坐标范围控制
    if (elementY.value < 50) {
      // 左侧
      position.top = -50
    } else if (elementY.value > 350) {
      // 右侧
      position.top = 250
    } else {
      // 中间
      position.top = elementY.value - 100
    }
    // 计算预览大图的移动的距离
    bgPosition.backgroundPositionX = -position.left - 50 * 2 + 'px'
    bgPosition.backgroundPositionY = -position.top - 50 * 2 + 'px'
    // 计算遮罩层的位置
    position.left = position.left + 50 + 'px'
    position.top = position.top + 50 + 'px'
  })
  // console.log('箭头', position.left, targets)
  return { position, bgPosition, isShow, targets }
}
export default {

  name: 'XtxGoodsPage',
  components: {
    Xinxi,
    gouwuche,
    diqu,
    relevant
    // goodsimg,

  },

  setup () {
    const store = useStore()
    const route = useRoute()
    const imgUrl = ref(null)
    const ress = ref({})
    const resss = ref([])
    const ressss = ref('null')
    const gouwuches = ref({})
    const jiages = ref(null)
    const jiagess = ref(null)
    const goodsimgs = ref(null)
    const imgsFn = () => {
      goodsimgs.value = imgUrl.value
    }
    const { position, bgPosition, isShow, targets } = usePreviewImage()
    console.log(route.params.id)

    findGoods(route.params.id).then(res => {
      console.log(res)
      ress.value = res.result
      ress.value.shuliang = 1
      resss.value = res.result.skus
      gouwuches.value = res.result.specs[0]
      imgUrl.value = ress.value.mainPictures[0]
      jiages.value = ress.value.skus[0].price
      jiagess.value = ress.value.skus[0].oldPrice
      ressss.value = gouwuches.value.values[0]
    })

    // ==========
    const jiage = (ind) => {
      jiages.value = ress.value.skus[ind].price
      jiagess.value = ress.value.skus[ind].oldPrice
      ressss.value = gouwuches.value.values[ind]
      // skus picture
      imgUrl.value = gouwuches.value.values[ind].picture
      console.log(jiages.value)
    }
    const ann = (a) => {
      console.log(a, 'ss')
      store.commit('calt/setcart', { ress, a })
    }
    const sumsFn = (a) => {
      console.log(a)
    }
    return {
      jiagess,
      jiages,
      resss,
      imgUrl,
      goodsimgs,
      imgsFn,
      ressss,
      gouwuches,
      position,
      bgPosition,
      isShow,
      ress,
      targets,
      jiage,
      ann,
      sumsFn

    }
  }
}
</script>

<style lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
 z-index: 500;

}
.large {
   position: absolute;
   top: 0;
   left: 412px;
   width: 400px;
   height: 400px;
   box-shadow: 0 0 10px rgba(0,0,0,0.1);
   background-repeat: no-repeat;
   background-size: 800px 800px;
   background-color: #f8f8f8;
   z-index: 5;
 }
.zhezhao{
  position: absolute;
  left: 220px;
  top: 0px;
  width: 100px;
  height: 100px;
  background-color: rgba(10, 7, 7 ,0.7);
}
.saimg{
  position: absolute;
  right:  -400px;
  top: 0px;
}
.xiangqinbox {
  .header {
    margin: 0 auto;
    width: 1240px;
  }

  .xian{
    border: 0.1px solid rgb(201, 195, 195);
    margin-top: 25px;
    height: 60px;
  }
}
.main {
  width: 1240px;
  margin: 0 auto;
  background-color: #fff;
  display: flex;

  .left {
    width: 580px;
    height: 600px;
    padding: 40px;
    .top{
position: relative;
    display: flex;
    ul{
//  overflow: auto;
      width: 80px;
      height: 400px;
      li{
            margin-left: 12px;
    margin-bottom: 15px;
   &:hover{
      border: 1px solid #0cc4ca;
   }
      }
    }
    .imgs{
 width: 400px;
    height: 400px;
 cursor:move;

img {

      width: 100%;
      height: 100%;
    }
    }

    }
    .buttom {
      text-align: center;
      display: flex;
      div {
        padding: 20px;
        a:hover{
          color: aquamarine;
        }
        .meiyuan {
          margin: 10px 0;
          color: red;
        }
      }
    }
  }

}
</style>
